﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AdornerTest
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:AdornerTest"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:AdornerTest;assembly=AdornerTest"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:IPTImageButton/>
    ///
    /// </summary>
    public class IPTImageButton : Button
    {
        public static readonly DependencyProperty ContentTextProperty;
        public static readonly DependencyProperty ImageWidthProperty;
        public static readonly DependencyProperty ImageHeightProperty;
        public static readonly DependencyProperty RectImageHeightProperty;
        public static readonly DependencyProperty NormalImageProperty;
        public static readonly DependencyProperty NormalImageSourceProperty;
        public static readonly DependencyProperty HoverImageProperty;
        public static readonly DependencyProperty PressedImageProperty;
        public static readonly DependencyProperty DisabledImageProperty;
        public static readonly DependencyProperty BorderVisibilityProperty;
        public string ContentText
        {
            get
            {
                return (string)base.GetValue(ContentTextProperty);
            }
            set
            {
                base.SetValue(ContentTextProperty, value);
            }
        }
        public double ImageWidth
        {
            get
            {
                return (double)base.GetValue(ImageWidthProperty);
            }
            set
            {
                base.SetValue(ImageWidthProperty, value);
            }
        }
        public double ImageHeight
        {
            get
            {
                return (double)base.GetValue(ImageHeightProperty);
            }
            set
            {
                base.SetValue(ImageHeightProperty, value);
            }
        }
        public double RectImageHeight
        {
            get
            {
                return (double)base.GetValue(RectImageHeightProperty);
            }
            set
            {
                base.SetValue(RectImageHeightProperty, value);
            }
        }
        public string NormalImage
        {
            get
            {
                return (string)base.GetValue(NormalImageProperty);
            }
            set
            {
                base.SetValue(NormalImageProperty, value);
            }
        }
        public System.Windows.Media.ImageSource NormalImageSource
        {
            get
            {
                return (System.Windows.Media.ImageSource)base.GetValue(NormalImageSourceProperty);
            }
            set
            {
                base.SetValue(NormalImageSourceProperty, value);
            }
        }
        public string HoverImage
        {
            get
            {
                return (string)base.GetValue(HoverImageProperty);
            }
            set
            {
                base.SetValue(HoverImageProperty, value);
            }
        }
        public string PressedImage
        {
            get
            {
                return (string)base.GetValue(PressedImageProperty);
            }
            set
            {
                base.SetValue(PressedImageProperty, value);
            }
        }
        public string DisabledImage
        {
            get
            {
                return (string)base.GetValue(DisabledImageProperty);
            }
            set
            {
                base.SetValue(DisabledImageProperty, value);
            }
        }
        public System.Windows.Visibility BorderVisibility
        {
            get
            {
                return (System.Windows.Visibility)base.GetValue(BorderVisibilityProperty);
            }
            set
            {
                base.SetValue(BorderVisibilityProperty, value);
            }
        }
        static IPTImageButton()
        {
            ContentTextProperty = DependencyProperty.Register("ContentText", typeof(string), typeof(IPTImageButton), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
            ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(IPTImageButton), new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.AffectsRender));
            ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(IPTImageButton), new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.AffectsRender));
            RectImageHeightProperty = DependencyProperty.Register("RectImageHeight", typeof(double), typeof(IPTImageButton), new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.AffectsRender));
            NormalImageProperty = DependencyProperty.Register("NormalImage", typeof(string), typeof(IPTImageButton), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(ImageSourceChanged)));
            NormalImageSourceProperty = DependencyProperty.Register("NormalImageSource", typeof(System.Windows.Media.ImageSource), typeof(IPTImageButton), new FrameworkPropertyMetadata(null));
            HoverImageProperty = DependencyProperty.Register("HoverImage", typeof(string), typeof(IPTImageButton), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(ImageSourceChanged)));
            PressedImageProperty = DependencyProperty.Register("PressedImage", typeof(string), typeof(IPTImageButton), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(ImageSourceChanged)));
            DisabledImageProperty = DependencyProperty.Register("DisabledImage", typeof(string), typeof(IPTImageButton), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(ImageSourceChanged)));
            BorderVisibilityProperty = DependencyProperty.Register("BorderVisibility", typeof(System.Windows.Visibility), typeof(IPTImageButton), new FrameworkPropertyMetadata(System.Windows.Visibility.Hidden, FrameworkPropertyMetadataOptions.AffectsRender));
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(IPTImageButton), new FrameworkPropertyMetadata(typeof(IPTImageButton)));
        }
        private static void ImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Application.GetResourceStream(new Uri("pack://application:,,," + (string)e.NewValue));
        }

    }
}
